<template>
	<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical" :submitLoading="submitLoading">
		<a-form-item label="表单标题：" name="formTitle">
			<a-input v-model:value="formData.formTitle" placeholder="请输入表单标题" allow-clear />
		</a-form-item>
		<a-form-item label="表单类型：" name="formType">
			<a-select v-model:value="formData.formType" placeholder="请选择表单类型" :options="formTypeOptions" />
		</a-form-item>
		<a-form-item label="表单路由：" name="formRouter" v-if="formData.formType == '2'">
			<a-input
				v-model:value="formData.formRouter"
				addon-before="src/views/flow/dtform/component/"
				addon-after=".vue"
				placeholder="请输入表单路由"
				allow-clear
			/>
		</a-form-item>
		<a-form-item label="描述：" name="remark">
			<a-textarea
				v-model:value="formData.remark"
				placeholder="请输入描述"
				:auto-size="{ minRows: 3, maxRows: 5 }"
				allow-clear
			/>
		</a-form-item>
		<a-form-item label="状态：" name="status">
			<a-switch :checked="formData.status === true" @change="editStatus" />
		</a-form-item>
		<a-form-item label="排序：" name="sort">
			<a-input v-model:value="formData.sort" type="number" placeholder="请输入排序" allow-clear />
		</a-form-item>
	</a-form>
</template>

<script setup name="testForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import sysFromConfigApi from '@/api/flow/sysFromConfigApi'
	// 抽屉状态
	const submitLoading = ref(false)
	const visible = ref(false)
	const props = defineProps({
		record: {
			type: Object,
			required: false
		}
	})
	const emit = defineEmits({ onBack: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const formTypeOptions = tool.dictList('FORM_TYPE')

	const initData = () => {
		if (props.record) {
			let recordData = cloneDeep(props.record)
			formData.value = Object.assign({}, recordData)
			formData.value.status = formData.value.status == '1' ? true : false
		}
	}
	initData()
	// 打开抽屉
	const onOpen = (record) => {
		submitLoading.value = true
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			formDataParam.status = formDataParam.status == true ? '1' : '0'
			sysFromConfigApi
				.sysFromConfigSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					emit('onBack')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	const editStatus = (status) => {
		formData.value.status = status
	}
	// // 默认要校验的
	const formRules = {
		formTitle: [required('请输入表单标题')]
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
<style scoped>
	.disable {
		pointer-events: none;
	}
</style>
